﻿using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using AT.IBMS.Framework.Tools;

namespace AT.IBMS.Framework.Logging
{
    public class WriteLogToFileProvider : WriteLogProviderBase
    {

        private string path;

        public override void Initialize(string name, NameValueCollection config)
        {
            path = config["filePath"];
            path = FrameworkManager.Tool.MapPath(path);
        }

        public override void Write(LogEntry log)
        {

            string splitLine;
            StringBuilder logText;
            StringBuilder category;
            StringBuilder properties;
            FileStream fs;
            StreamWriter writer;

            logText = new StringBuilder();
            category = new StringBuilder();
            properties = new StringBuilder();

            splitLine = "---------------------------------------------------------------";

            logText.AppendLine(splitLine);
            logText.AppendLine("Id: {@Id@}");
            logText.AppendLine("Timestamp: {@timestamp@}");
            logText.AppendLine("Message: {@message@}");
            logText.AppendLine("Category: {@category@}");
            logText.AppendLine("Priority: {@priority@}");
            logText.AppendLine("EventId: {@eventid@}");
            logText.AppendLine("Severity: {@severity@}");
            logText.AppendLine("Title:{@title@}");
            logText.AppendLine("Machine: {@machine@}");
            logText.AppendLine("Application Domain: {@appDomain@}");
            logText.AppendLine("Process Id: {@processId@}");
            logText.AppendLine("Process Name: {@processName@}");
            logText.AppendLine("Thread Name: {@threadName@}");
            logText.AppendLine("Extended Properties: {@dictionary@}");
            logText.AppendLine(splitLine);
            logText.AppendLine("");

            logText.Replace("{@Id@}", log.Id);
            logText.Replace("{@timestamp@}", log.TimeStamp.ToString("yyyy-MM-dd HH:mm:ss"));
            logText.Replace("{@message@}", log.Message);
            logText.Replace("{@priority@}", log.Priority.ToString());
            logText.Replace("{@eventid@}", log.EventId.ToString());
            logText.Replace("{@severity@}", log.Severity.ToString());
            logText.Replace("{@title@}", log.Title);
            logText.Replace("{@machine@}", log.MachineName);
            logText.Replace("{@appDomain@}", log.AppDomainName);
            logText.Replace("{@processId@}", log.ProcessId);
            logText.Replace("{@processName@}", log.ProcessName);
            logText.Replace("{@threadName@}", log.ManagedThreadName);

            foreach (string item in log.Categories)
            {

                if (category.Length > 0)
                {
                    category.Append(",");
                }

                category.Append(item);
            }

            if (log.ExtendedProperties != null)
            {

                foreach (KeyValuePair<string, string> item in log.ExtendedProperties)
                {
                    properties.Append(item.Key);
                    properties.Append(" - ");
                    properties.AppendLine(item.Value);
                }

            }

            if (properties.Length > 0)
            {
                properties.Insert(0, "{");
                properties.Append("}");
            }
            else
            {
                properties.Append("{}");
            }

            logText.Replace("{@category@}", category.ToString());
            logText.Replace("{@dictionary@}", properties.ToString());

            using (fs = new FileStream(path, FileMode.Append, FileAccess.Write))
            {
                using (writer = new StreamWriter(fs, Encoding.Default))
                {
                    writer.Write(logText.ToString());
                    writer.Flush();
                    writer.Close();
                }
            }

        }

    }
}
